package com.qysoft.modules.xxwh.service;

import com.qysoft.rapid.dao.mybatis.RapidDao;
import com.qysoft.rapid.domain.Bean;
import com.qysoft.rapid.domain.CurrentRyxx;
import com.qysoft.rapid.exceptions.BizException;
import com.qysoft.rapid.utils.StringUtils;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

import java.io.UnsupportedEncodingException;
import java.util.List;

/**
 * 人员管理services
 * @author wyg
 */
public class RYGLService {

    /**
     * 删除人员信息
     * @param id 人员代码
     * @throws Exception 
     */
    public static void delete(String id) throws Exception {
    	String sfmr = (String) RapidDao.queryOne("select flag from sys_user where id = ?", id);
    	if ("Y".equals(sfmr.toLowerCase())) {
			throw new BizException("删除失败，无法删除系统默认账户");
		}
        RapidDao.deleteById("sys_user_role", "userid",id);
        RapidDao.deleteById("sys_user", "id",id);
    }
	/**
	 * 查询角色信息
	 * @return
	 * @throws Exception
	 */
	public static List<Bean> queryJSXX() throws Exception{
		List<Bean> jsxx = RapidDao.queryList("select id ,name  from sys_role where 1=1 order by id asc");
		return   jsxx;
	}
	
    /**
     * 新增人员
     *
     * @param ryxx
     * @throws Exception
     */
    public static void saveRYXX(Bean ryxx) throws Exception {
    	Object count =  RapidDao.queryOne("select count(1) from sys_user t where t.id = ?", ryxx.getStr("id"));
        if (Long.parseLong(String.valueOf(count)) > 0) {
            throw new BizException("保存失败，重复的人员代码！");
        }
        ryxx.set("password",StringUtils.getMD5_SALT("123456"));
        RapidDao.insertByXml("insertRyXX",  ryxx);
    }

    /**
     * 查询机构信息
     *
     * @param id 人员代码
     * @return
     * @throws Exception
     */
    public static Bean queryJGXX(String id) throws Exception {
    	String sql="select  t.id ,t.orgid ,t.qx_orgid ,t.name ,t.username ,t.password ,"
    			+ "to_char(t.valid_start,'yyyy-MM-dd') as valid_start ,to_char(t.valid_end,'yyyy-MM-dd') as valid_end  ,t.identity,t.address, t.postcode,"
    					+ "t.telephone ,t.cellphone ,t.email ,t.flag ,t.photo,t.qq ,  (select name from sys_org where id = t.orgid)  as orgmc  from sys_user t  where t.id =?";
        Bean ryxx = (Bean) RapidDao.queryOne( sql, id);
        return ryxx;
    }

    /**
     * 修改人员信息
     *
     * @param ryxx
     */
    public static void updateRYXX(Bean ryxx) throws Exception {
        //删除不可修改项
        //ryxx.clear("orgid");
        RapidDao.updateByXml("RYGL.updateBy",   ryxx);
    }
    /**
     * 修改密码
     *
     * @param ryxx
     */
    public static void updateMmRYXX(Bean ryxx) throws Exception {
        //删除不可修改项
        //ryxx.clear("orgid");
        RapidDao.updateByXml("RYGL.updateMm",   ryxx);
    }
    
    /**
     * 根据人员代码查询人员信息
     * @param username 人员代码
     * @return
     * @throws Exception
     */
    public static Bean queryRyByDm(String username) throws Exception{
    	Bean ryxx = RapidDao.findById("sys_user", "username", username);
    	return ryxx;
    }
    
    /**
     * 保存授权的人员角色
     * @param userid 人员代码
     * @param jsids 角色id，复合以，分隔
     * @throws Exception
     */
	public static void saveRyJs(String userid,String jsids) throws Exception{
		//删除所有已授权的角色信息
		RapidDao.deleteById("sys_user_role", "userid", userid);
		//重新保存授权
		if ("".equals(jsids)) {
			return;
		}
		String[] arrJSID = jsids.split(",");
		for(int i=0;i<arrJSID.length;i++){
			RapidDao.executeSql("insert into sys_user_role (userid,roleid) values (?,?)", userid,arrJSID[i]);
		}
	}    
    
    /**
     * 根据人员代码查询人员信息并绑定到当前登录用户
     * @param id 人员代码
     * @return
     * @throws Exception
     */
    public static CurrentRyxx queryCurrnetRyxxByDm(String id) throws Exception{
    	CurrentRyxx currentUser = (CurrentRyxx) RapidDao.selectOneByXml("currentRyxx_select", id);
    	return currentUser;
    }	
    /**
	   *  解密  
	   * @param s
	   * @return
	   */
public static String getFromBase64(String s) {  
    byte[] b = null;  
    String result = null;  
    if (s != null) {  
        BASE64Decoder decoder = new BASE64Decoder();  
        try {  
            b = decoder.decodeBuffer(s);  
            result = new String(b, "utf-8");  
        } catch (Exception e) {  
            e.printStackTrace();  
        }  
    }  
    return result;  
}  
/**
 * 加密  
 * @param str 
 * @return
 */
public static String getBase64(String str) {  
    byte[] b = null;  
    String s = null;  
    try {  
        b = str.getBytes("utf-8");  
    } catch (UnsupportedEncodingException e) {  
        e.printStackTrace();  
    }  
    if (b != null) {  
        s = new BASE64Encoder().encode(b);  
    }  
    return s;  
}  

/**
 * 获取原密码数据
 * @param id
 * @return
 * @throws Exception 
 */
public static String queryYmm(String id) throws Exception {
	Bean b = RapidDao.findById("sys_user", "id",id);
    String str=String.valueOf(b.get("password"));
	return str;
}
/**
 * 修改密码信息
 * @param bean
 * @throws Exception 
 */
public static void updateMM(Bean bean) throws Exception {
	RapidDao.updateByXml("RYGL.updateMM", bean);
}

 


}
